package com.cswnode.Service.impl;

import com.cswnode.Dao.MessageMapper;
import com.cswnode.Entity.Message;
import com.cswnode.Service.MessageService;
import com.cswnode.Util.Constant;
import com.cswnode.Util.SensitiveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author csw
 * @create 2020/9/26 13:48
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private SensitiveUtil sensitiveUtil;

    @Override
    public List<Message> findConversations(int useId, int offset, int limit) {
        return messageMapper.selectConversations(useId, offset, limit);
    }

    @Override
    public int findConversationcount(int userId) {
        return messageMapper.selectConversationCount(userId);
    }

    @Override
    public List<Message> findPrivateMessageList(String conversationId, int offset, int limit) {
        return messageMapper.selectPrivateMessageList(conversationId, offset, limit);
    }

    @Override
    public int findPrivateMessageCount(String conversationId) {
        return messageMapper.selectPrivateMessageCount(conversationId);
    }

    @Override
    public int findsUnreadMessage(int userId, String conversationId) {
        return messageMapper.selectUnreadMessage(userId, conversationId);
    }

    @Override
    public int insertMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveUtil.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    @Override
    public int updateMessageStatus(List<Integer> ids, int status) {
        return messageMapper.updateMessageStutas(ids, Constant.HAVE_READ);
    }
}
